/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view;

import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.tapestry.IExternalPage;
import org.apache.tapestry.IRequestCycle;
import org.braintrain.domain.*;
import org.braintrain.services.util.contentBaseVisitor.ContentBaseTestDataVisitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * User: Robert H. Engelhardt
 * Date: 4/12/11
 * Time: 1:46 PM
 */
public abstract class AnswerReport extends BrainTrainSecuredBasePage implements IExternalPage {



    public abstract Module getModule();
    public abstract void setModule(Module module);

    public abstract Collection<TestData> getTestData();
    public abstract void setTestData(Collection<TestData> testData);

    public abstract TestData getCurrentTestData();
    public abstract void setCurrentTestData(TestData currentTestData);

    public abstract Question getCurrentQuestion();
    public abstract void setCurrentQuestion(Question currentQuestion);

    public abstract Answer getCurrentAnswer();
    public abstract void setCurrentAnswer(Answer currentAnswer);

    public void activateExternalPage(Object[] objects, IRequestCycle requestCycle) {

        if(objects == null || objects.length == 0 || !(objects[0] instanceof Long)) {
            throw new ApplicationRuntimeException("AnswerReport not activated with expected parameters");
        }

        Long moduleId = (Long)objects[0];
        Module module = getGenericService().getById(Module.class, moduleId);

        if(module == null) {
            throw new ApplicationRuntimeException("Unknown module with id of " + moduleId);
        }

        setModule(module);

        Collection<TestData> testData = collectAllTestData(module);

        setTestData(testData);

    }

    private Collection<TestData> collectAllTestData(Module module) {

        List<Navigation> navigationList = getNavigationService().getMainNavigationChain(module);
        List<List<Navigation>> linkedChains = getNavigationService().getLinkedChains(module, false, false);

        for(List<Navigation> linkedChain: linkedChains) {
            navigationList.addAll(linkedChain);
        }

        Collection<TestData> testDataCollections = new ArrayList<TestData>();

        for(Navigation navigation: navigationList) {
            ContentBaseTestDataVisitor visitor = new ContentBaseTestDataVisitor(navigation);
            navigation.getContent().getValue().accept(visitor);
            if(visitor.isTest()) {
                testDataCollections.add(visitor.getTest());
            }
        }

        return testDataCollections;

    }

//    public String getAnswerReportUrl() {
//
//        IEngineService service = getServiceMap().getService(AnswerHitService.SERVICE_NAME);
//        ILink link = service.getLink(false, Collections.<Object, Object>emptyMap());
//        return link.getURL();
//    }

}
